<?php

namespace App\Http\Controllers;

use Phalcon\Mvc\Controller;
use App\Util\Fun;

class BaseController extends Controller
{
	protected $f;
	protected $city_id;

	//初始化
	protected function initialize(){
		$this->f = new Fun;
		$this->getCityId();
		$this->queue->put(json_encode([
			"command" => "REQ",
			'city_id' => $this->request->getHeader('City-Id'),
			'channel_id' => $this->request->getHeader('Channel-Id'),
			'uri' => $this->request->getURI(),
			'raw'=> $this->request->getJsonRawBody(),
			'sub'=> $this->session->get('sub'),
            'ip'=> $this->request->getClientAddress(),
            'ua'=> $this->request->getUserAgent(),
            //'token' => $this->request->getHeader('Authorization'),
		]));
	}

	//根据头部获取city_id 默认为成都 63
	protected function getCityId()
	{
		$city_id = $this->request->getHeader('City-Id');
		$this->city_id = $city_id ? intval($city_id) : '63';
	}

	//验证数据
	protected function validation($validation,$raw){
		if(empty($raw)){
			$raw = new \stdClass();
		}
		$messages = $validation->validate($raw);
		if(count($messages)){
			$i=0;
			$msg = [];
			foreach ($messages as $message) {
				++$i;
				$msg[]="$i: $message;";
			}
			$msg = implode('',$msg);
			$this->echoJson($msg,401);
		}
		$data = !$validation->getData() ?  new \stdClass() : $validation->getData();
		if(property_exists($this,'user') && $this->user ){
			$data->member_id = $this->user->member_id;
			$data->avatar = isset($data->avatar) ? $data->avatar : $this->user->avatar;
			$data->nickname = isset($data->nickname) ? $data->nickname : $this->user->nickname;
		}
		return $data;

	}

	protected function echoJson($data,$code = 0){
		empty($data) && $data = new \stdClass();
		if(is_array($data)){
			isset($data['list']) && empty($data['list']) && $data['list'] = [];
		}
		$this->f->echoJson($data,$code);
	}

	//获取用户
	protected function getUser($auth = 1){
		$sub = $this->session->get('sub');
		if($auth && empty($sub)){
			$this->echoJson("jwt not empty",401);
		}
		$this->user = $sub;
		return $sub;
	}

	protected function sql(){
		$sql = $this->di->get('profiler')->getProfiles();
		print_r($sql);
		exit;
	}

	/**
	 * [getFullUrl 获取完整url]
	 * @param  [type]  $old_url       [原始url]
	 * @param  boolean $is_https      [是否采用https]
	 * @param  [type]  $self_prefix   [采用自定义前缀]
	 * @return [type]                 [description]
	 */
	public function getFullUrl($old_url, $is_https = FALSE, $self_prefix = NULL)
	{
		return $this->f->getFullUrl($old_url, $is_https, $self_prefix);
	}

	/**
	 * [getShowTime 时间构造]
	 * @param  [type] $time [description]
	 * @return [type]       [description]
	 */
	public function getShowTime($time)
    {
    	if (!$time)
    		return date("y-m-d", time() + 8 * 60 * 60);

    	$diff = time() - strtotime($time) + 8 * 60 * 60;

    	//3分钟以内 显示刚刚
    	if ($diff <= 60*3)
    		return "1分钟前";

    	//5分钟以内 显示1分钟前
    	if ($diff <= 60*5)
    		return "3分钟前";

    	//10分钟以内 显示5分钟前
    	if ($diff <= 60*10)
    		return "5分钟前";

    	//10分钟到半小时 10
    	if ($diff <= 60*30)
    		return "10分钟前";
    	
    	//半小时到1小时 半小时前
    	if ($diff <= 60*60)
    		return "半小时前";
    	
    	//1-3小时  1小时前
    	if ($diff <= 60*60*3)
    		return "1小时前";
    	
    	//3-5小时  3小时前
    	if ($diff <= 60*60*5)
    		return "3小时前";
    	
    	//5-12小时 5小时前
    	if ($diff <= 60*60*12)
    		return "5小时前";

    	//12-15小时 12小时前
    	if ($diff <= 60*60*15)
    		return "12小时前";

    	//15-24小时 3小时前
    	if ($diff <= 60*60*24)
    		return "24小时前";

    	//其他显示日期
    	return date("m月d日", strtotime($time));
    }

    /**
     * [getIdArray array_map 回掉获取id数组]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function getIdArray($data)
	{
		if (is_array($data))
			return $data['id'];
		if (is_object($data))
			return $data->id;
	}
    protected function getRandCode($length=32,$mode=0){
        return $this->f->getCode($length,$mode);

    }
    //发送营销短信接入
    protected function sendSaleSms($data){

        $data=$this->sendSmsInitParamterSetting($data);

        return $this->f->postCurl("http://210.14.150.20:8888/sms.aspx",$data);
    }
    protected function sendSmsInitParamterSetting($data){
        $data['userid']=147;
        $data['account']="启源生态";
        $data['password']="123456";
        $data['sendTime']="";
        $data['action']="send";
        $data['extno']='';

        return $data;
    }
}
